﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Repos.InMemory
{
    public class InMemoryCourseLoader : ICourseLoader
    {
        private static List<Course> CourseList;

        public InMemoryCourseLoader ()
	    {
            if (CourseList == null)
            {
                CourseList = new List<Course>();
                Course course1 = new Course() { ID = "DVGA24", Name = "Datavetenskapens Grunder", Points = 7.5d };
                Course course2 = new Course() { ID = "DVGC01", Name = "Programming Language", Points = 7.5d };

                course1.Department = new Department() { ID = 1, Name = "Computer Science" };
                course2.Department = new Department() { ID = 2, Name = "Computer Science" };
                course1.Administrator = new User() { ID = "MBl", Department = course1.Department, Email = "MBl@lkau.se", FirstName = "Martin", LastName = "Blom", Password = "", Phone = "070-07070707" };
                course1.Counselor = new User() { ID = "DFR", Department = course1.Department, Email = "DFR@kau.se", FirstName = "Donald", LastName = "Ross", Password = "", Phone = "707-01010101" };
                course2.Administrator = new User() { ID = "DFR", Department = course2.Department, Email = "DFR@kau.se", FirstName = "Donald", LastName = "Ross", Password = "", Phone = "707-01010101" };
                course2.Counselor = new User() { ID = "MBl", Department = course1.Department, Email = "MBl@lkau.se", FirstName = "Martin", LastName = "Blom", Password = "", Phone = "070-07070707" };
                course1.Subject = new Subject() { ID = "DV", Name = "Computer Science" };
                course2.Subject = new Subject() { ID = "IS", Name = "Informatics" };
                CourseList.Add(course1);
                CourseList.Add(course2);
            }
	    }

        public List<Course> GetCourses()
        {


            return CourseList;
        }


        public bool CourseExists(string CourseID)
        {
            foreach (Course course in CourseList) {
                if (CourseID == course.ID)
                {
                    return true;
                }
            }
            return false;
        }


        public List<Course> SearchCoursesByPatten(string Pattern)
        {
            var courses = GetCourses();
            return courses.Where(u => u.ID.Contains(Pattern) || u.Name.Contains(Pattern)).ToList<Course>();
        }

        public void InsertCourse(Course course)
        {
            CourseList.Add(course);
        }
        public void DeleteCourse(string CourseID)
        {
            for (int i = 0; i < CourseList.Count; i++)
            {
                if (CourseList[i].ID == CourseID)
                    CourseList.RemoveAt(i);
            }
        }

        public void EditCourse(Course Course)
        {
            DeleteCourse(Course.ID);
            InsertCourse(Course);
        }


        
        public Course GetCourseByID(string ID)
        {
            foreach (Course c in CourseList)
                if (c.ID.ToLower() == ID.ToLower())
                    return c;
            return null;
        }

        public void Clear()
        {
            CourseList = null;
        }
    }

}